Flash Article

Building a Tip of the Day Application (Part 3): Submitting Tips to the Web Service


Table of Contents

  1. Introduction
  2. Binding Data to the Submit a Tip Interface
  3. Formatting Dates
  4. Building the Calendar Browse Interface
  5. Searching for Tips
  6. Displaying a Tip from the Search Results
  7. Applying a Style Sheet

Displaying a Tip from the Search Results

When a user gets a list of results from the keyword search, you want him to be able to view the result by clicking the tip’s name.

  1. Write code to load a tip when a user clicks a search result.

    1. Select the results_ls List component instance on the searchresult screen.
    2. Open the Actions panel and add the following code onto the results_ls instance:
      this._parent._parent._parent.dataHolder_dh.tipdate_str = this.selectedItem.data;

    Note: The data field of the List component holds preformatted date information from the web service. You can bind the data directly to the DataHolder component instead of having to call the custom dateFormat() function in Macromedia Flash to format the date for you.

  2. Add a behavior to load the tip screen when a user selects a tip from the List instance.

    1. Select the results_ls instance on the Stage.
    2. Click the Add Behavior button in the Behaviors panel and select Screens > Go to Slide from the contextual menu.
    3. Select the tip screen and click OK to close the dialog box.
    4. Make sure the Go to Slide event is set to change in the pop-up menu.
    5. Check that the code you added to set the tipdate_str variable in the DataHolder_dh instance comes before the code that loads a new screen but after the on change() handler, like this:
      on (change)
          this._parent._parent._parent.dataHolder_dh.tipdate_str = this.selectedItem.data;
          // GoTo Screen behavior
      
  3. Select Control > Test Movie to test the SWF file.

    Now you can perform a keyword search, click a result in the List component instance, and be redirected to the home page with the new tip loading in.

  4. Add code to check the number of values being returned by the XML packet and display a logical error informing the user that no results were found.

    1. Click the presentation screen and click the getTipsByKeyword_wsc instance.
    2. Replace the existing code on the instance with the code below:

      on (result) {
          var dh = this._parent.dataHolder_dh;
          XML.prototype.ignoreWhite = true;
          dh.keywordresults = new XML(this.results);
          if (dh.keywordresults.firstChild.childNodes.length == 0) {
              dh.keywordresults = new XML("<results><tip><TipTitle>No search results 
          found.</TipTitle><TipDateTime /><ProductIcon /></tip></results>");
      	}
      }
      

    The above code first creates a shortcut variable (dh) to the dataHolder_dh instance. This simplifies your subsequent code; you don't have to type out the full path to the component each time. Next, the if statement checks to see if the web service passes back any results. It does this by checking the length of the childNodes array, a Flash XML object Flash creates from the XML data returned by the web service. If the number of results equals zero, you create your own XML packet which will be bound to the List component. It will say No search results found rather than displaying an empty List with no results. This way, the user knows that the search successfully completed, but they just didn’t get any matching results.

  5. Disable the List component if no search results were returned.

    Although it seems like everything is possibly working, there is still one strange quirk in the application. If no search results are returned, the app displays an error message, but a user can still click on the No results were found message and generate an error when Flash tries to connect to the web service. This happens because the data field for the error message didn’t contain a date.

    There are three possible solutions to this problem:

    1. Make up a date to pass in the error message. (This is not a very good solution because the date wouldn’t be meaningful at all.)
    2. Check that the date is valid before continuing with the on (change) for the List component.
    3. Disable the List component if no results were found to prevent the user from clicking anything.

    Both 2 and 3 are acceptable solutions, but because option 3 makes for a more interesting article and neither of the options are more than a couple lines of code, you’re stuck reading about the last option!

    If you want to disable a List component (gray it out) or toggle whether a List component is selectable, modify the code on the getTipsByKeyword_wsc.

    1. Select getTipsByKeyword_wsc on the Stage.
    2. Open the Actions panel and change the if statement to the following snippet:

      if (dh.keywordresults.firstChild.childNodes.length == 0) {
          dh.keywordresults = new XML("<results><tip><TipTitle>No search results
          found.</TipTitle><TipDateTime /><ProductIcon /></tip></results>");
          this._parent.searchform.searchresult.results_ls.selectable = false;
      	} else {
          this._parent.searchform.searchresult.results_ls.selectable = true;
      	}
      

    You’ve set the List component’s ability to be selectable based on the condition in the if statement. You've added an else condition as well. This prevents a user from being able to select the error message.

    results_ls List component instance with enabled set to false

    Figure 7. results_ls List component instance with enabled set to false

  6. Note: Even though users cannot select the error message, they can still mouse over it. The error message will highlight, however, it won’t react to mouse clicks. If you find this behavior confusing, you can replace selectable with enabled. This makes the List component gray out.